package com.jason.quartzdemo.config;


import com.jason.quartzdemo.job.Job_A;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
/**
 * 重定义 SchedulerFactoryBean 加载自定义的配置文件
 * 使用默认的SchedulerFactoryBean 也可以执行定时器
 * 查看 https://www.jianshu.com/p/13653be64442
 */
@Configuration
public class QuartzConfig {



    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setConfigLocation(new ClassPathResource("/quartz.properties"));


        schedulerFactoryBean.setTriggers(trigger2());
        schedulerFactoryBean.setJobDetails(jobDetail());
        return schedulerFactoryBean;
    }



    @Bean
    public JobDetail jobDetail(){

        return JobBuilder.newJob(Job_A.class)
                .withIdentity("job1","group_1")
                .storeDurably(true)
                .build();
    }

    /*@Bean
    public Trigger trigger(){

        return TriggerBuilder.newTrigger()
                .startNow()
                .withIdentity("job1","group_2")
                .forJob(jobDetail())
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
                .build();
    }*/

    @Bean("trigger2")
    public Trigger trigger2(){

        return TriggerBuilder.newTrigger()
                .startNow()
                .withIdentity("job1","group_1")
                .forJob(jobDetail())
                .withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?"))
                .build();
    }

}
